package view.inventory;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.swing.table.AbstractTableModel;
import model.inventory.ItemHpp;

public class CopyHargaTM extends AbstractTableModel{
    private List<ItemHpp> data;

    public CopyHargaTM() {
        this(new ArrayList<ItemHpp>(0));
    }

    public CopyHargaTM(List<ItemHpp> data) {
        this.data = data;
    }
     
    public ItemHpp get(int index) {
        return data.get(index);
    }

    public void add(ItemHpp product) {
        data.add(product);
        fireTableRowsInserted(getRowCount() - 1, getRowCount() - 1);
    }

    public void remove(int index) {
        data.remove(index);
        fireTableRowsDeleted(index, index);
    }

    public void set(int index, ItemHpp product) {
        data.set(index, product);
        fireTableRowsUpdated(index, index);
    }

    public void clear() {
        data.clear();
        fireTableDataChanged();
    }

    public void add(Collection<ItemHpp> products) {
        data.addAll(products);
        fireTableRowsInserted(getRowCount() - (products.size()), getRowCount() - 1);
    }

    @Override
    public boolean isCellEditable(int row, int col) {
        return col == 3 ? true: false;
    }
    
    @Override
    public int getRowCount() {
        return data.size();
    }

    @Override
    public int getColumnCount() {
        return 5;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
         switch (columnIndex) {   
            case 0: return rowIndex+1;
            case 1: return data.get(rowIndex).getItem().getPlu();
            case 2: return data.get(rowIndex).getItem().getNama();
            case 3: return data.get(rowIndex).isSelect();
            case 4: return "";
            default: return null;
        }
    }
    
    @Override
    public void setValueAt(Object value, int rowIndex, int columnIndex) {
         if (columnIndex==3) {
            data.get(rowIndex).setSelect((Boolean)value);
            fireTableCellUpdated(rowIndex, columnIndex);
         }
    }
    
    @Override
    public String getColumnName(int column) {
        switch (column) {
            case 0: return "NO";
            case 1: return "PLU";
            case 2: return "NAMA";            
            case 3: return "V";
            case 4: return "";
            default: return null;
        }
    }
    
    @Override
    public Class getColumnClass(int column) {
        return getValueAt(0, column).getClass() ;
    }
    
}
